package com.oocl.consume.parsetoobject;


import java.util.ArrayList;
import java.util.Date;
import java.util.List;



import org.json.JSONObject;

import com.oocl.consume.dao.imp.BookDaoImp;
import com.oocl.consume.pojo.Book;

public class ParseToObject {
	public static List<Book> listBook = new ArrayList<Book>();
	public static List<Book> parseToBookList(List<String> list) throws Exception {
		for(String s:list){
			Book book = new Book();
			JSONObject jso = new JSONObject(s);
			book = parseToBook(jso);
			listBook.add(book);
		}
		return listBook;
	}
	public static List<Book> parseToBookList(String msg) throws Exception {
		Book book = new Book();
		JSONObject jso = new JSONObject(msg);
		book = parseToBook(jso);
		System.out.println(book.toString());
		BookDaoImp bookDaoImp = new BookDaoImp();
		bookDaoImp.addBook(book);
		listBook.add(book);
		return listBook;
	}
	private static Book parseToBook(JSONObject jso) throws Exception {
		Book book = new Book();
		book.setAuthor((String) jso.get("author"));
		book.setFineness((Boolean) jso.get("fineness"));
		book.setId((Integer) jso.get("id"));
		book.setName((String) jso.get("name"));
		book.setPrice((Double) jso.get("price"));
		book.setPublicationDate( new Date((Long)jso.get("publicationDate")));
		book.setPublisher((String) jso.get("publisher"));
		return book;
	}
	
	
}
